<?php
require_once 'Zend/Db/Table/Abstract.php';
class Model_DishMapper extends Zend_Db_Table_Abstract
{
	protected $_name='dish';
	protected $_primary='DishID';
	protected $_referenceMap = array (
			'category' => array (
					'columns' => array ('CatID'),
					'refTableClass' => 'Model_CategoryMapper',
					'refColumns' => array ('CatID')
			)
	);
	
	protected $_dependentTables = array('dish_location');
	
	public function getAllDishes()
	{
		$rowset=$this->fetchAll();
		$rowcount= count($rowset);
		$result=Array();
		$i=0;
		if($rowcount>0)
		{
			foreach ($rowset as $row)
			{
				$i=$i+1;
				$name = $row->Name;
				$id = $row->DishID;
				$catId=$row->CatID;
				$image=$row->Image;
				$catMapper=new Model_CategoryMapper();
				$category=$catMapper->getCategory($catId);
				
				$description=$row->Description;
				$dish=new Model_Dish();
				$dish->setId($id)->setName($name)->setCategory($category)->setDescription($description)->setImage($image);
				$result[$i]=$dish;
			}
		}
		return $result;
	}
	
	public function findDishes($query)
	{
		$select = $this->select();
		$select->where('Name LIKE ?', '%'.$query.'%');
		$rowset = $this->fetchAll($select);
		
		$rowcount= count($rowset);
		$result=Array();
		$i=0;
		if($rowcount>0)
		{
			foreach ($rowset as $row)
			{
				$i=$i+1;
				$name = $row->Name;
				$id = $row->DishID;
				$catId=$row->CatID;
				$image=$row->Image;
				$catMapper=new Model_CategoryMapper();
				$category=$catMapper->getCategory($catId);
	
				$description=$row->Description;
				$dish=new Model_Dish();
				$dish->setId($id)->setName($name)->setCategory($category)->setDescription($description)->setImage($image);
				$result[$i]=$dish;
			}
		}
		return $result;
	}
	
	public function getDish($dishId)
	{
		$row=$this->fetchRow($this->select()->where('DishID=?',$dishId));
		
		$result=new Model_Dish();
		
		if($row)
		{
			
				$name=$row->Name;
				$id=$row->DishID;
				$catId=$row->CatID;
				$image=$row->Image;
				$catMapper=new Model_CategoryMapper();
				$category=$catMapper->getCategory($catId);
	
				$description=$row->Description;
				$dish=new Model_Dish();
				$dish->setId($id)->setName($name)->setCategory($category)->setDescription($description)->setImage($image);
			    $result=$dish;
		}
		return $result;
	}
	
	public function getDishesByCategory($catId)
	{
		$rowset=$this->fetchAll($this->select()->where('CatID=?',$catId))->toArray();
		$rowcount= count($rowset);
		$result=Array();
		$i=0;
		if($rowcount>0)
		{
			foreach ($rowset as $row)
			{
				$i=$i+1;
				$name = $row['Name'];
				$id = $row['DishID'];
				$catId=$row['CatID'];
				$image=$row['Image'];
				$catMapper=new Model_CategoryMapper();
				$category=$catMapper->getCategory($catId);
	
				$description=$row['Description'];
				$dish=new Model_Dish();
				$dish->setId($id)->setCategory($category)->setName($name)->setDescription($description)->setImage($image);
				$result[$i]=$dish;
			}
		}
		return $result;
	}
	
	public function getTopDishes()
	{
		$rowset=$this->fetchAll()->toArray();
		$rowcount= count($rowset);
		$result=Array();
		$i=0;
		if($rowcount>0)
		{
			foreach ($rowset as $row)
			{
				$i=$i+1;
				$name = $row['Name'];
				$id = $row['DishID'];
				$catId=$row['CatID'];
				$image=$row['Image'];
				$catMapper=new Model_CategoryMapper();
				$category=$catMapper->getCategory($catId);
	
				$description=$row['Description'];
				$dish=new Model_Dish();
				$dish->setId($id)->setCategory($category)->setName($name)->setDescription($description)->setImage($image);
				$result[$i]=$dish;
				if ($i>3) return $result;
			}
		}
		return $result;
	}
	
	public function getTotalDishesByCategory($catId){
		$rowset=$this->fetchAll($this->select()->where('CatID=?',$catId))->toArray();
		$rowCount = count($rowset);
		return $rowCount;
	}
	
	public function createDish(Model_Dish $dish)
	{
		try {
			$data=array(
					'CatID'     	=> $dish->getCategory()->getId(),
					'Name' 			=> $dish->getName(),
					'Image'      	=> $dish->getImage(),
					'Description'	=> $dish->getDescription(),		
			);
			$this->insert($data);
			return "da tao";
		} catch (Exception $e) {
			return "error";
		}
		
	}
	
	public function updateDish(Model_Dish $dish)
	{
	
		try {
			$data=array(
					'Name'     => $dish->getName(),
					'CatID' 	=> $dish->getCategory()->getId(),
					'Image'      => $dish->getImage(),
					'Description'	=> $dish->getDescription(),
			);
			$where=Array('DishID = ?' => $dish->getId()
			);
			$this->update($data, $where);
			return "";
		} catch (Exception $e) {
			return "error";
		}
	
	
	}
	
	
	public function removeDish($dishId)
	{
		try {
			$registry 	= Zend_Registry::getInstance();
			$DB = $registry['my_db'];
			$DB->delete('Dish', 'DishID = '.$dishId);
			return true;
		}
		catch (Exception $e){
			return  false;
		}
		

	}

}
	
	
